package com.demo.application.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    private int count = 0;

    @PostMapping("/hello")
    public String hello() {
        if (count % 2 == 0) {
            System.out.println("hello=====================" + (count++));
            logger.info("===============task need to cost 6s.===============");
            sleepTime(6);
        } else {
            System.out.println("hello=====================" + (count++));
            logger.info("===============task need to cost 3s.===============");
            sleepTime(3);
        }
        return "hello";
    }

    @GetMapping("/")
    public String getInfo() {
        return "Application-B";
    }

    private void sleepTime(int seconds) {
        try {
            Thread.sleep(seconds*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }


}
